#ifndef STUDENT_LIB_H_
#define STUDENT_LIB_H_

#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>

#include "Student.h"

using namespace std;

void Add_student(string s, Student l[], int &numberStudent){
    string temp, a_fname, a_dob, a_addr, a_classname, a_program;
    int a_id, a_year;

    int mark = s.find(":");
    a_id = atoi(s.substr(0,mark).c_str());

    temp = s.substr(mark+1);
    mark = temp.find(":");
    a_fname = temp.substr(0,mark);

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_dob = temp.substr(0,mark);

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_addr = temp.substr(0,mark);

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_year = atoi(temp.substr(0,mark).c_str());

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_program = temp.substr(0,mark);

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_classname = temp.substr(0,mark);

    Student Stu(a_id, a_fname, a_dob, a_addr, a_year, a_program, a_classname);
    l[numberStudent] = Stu;
    numberStudent = numberStudent + 1;
}

void Read_student(string _file, Student l[], int &numberStudent){
    ifstream fin;
    fin.open(_file.c_str());
    if(!fin.is_open()){
        cout<<"Cannot open file.\n";
        exit(1);
    }else{
        string s = "";
        while (getline(fin, s)){
            Add_student(s,l,numberStudent);
        }
        fin.close();
    }
}

Student Get_student_by_id(int key, Student l[], int numberStudent){
    int i = 0;
    Student result(0, "", "", "", 0, "", "");
    for (i = 0; i <numberStudent; i++){
        if (l[i].Get_id() == key){
            return l[i];
        }
    }

    return result;
}

void Get_students_by_name(string key, Student l[], int numberStudent, Student result[], int &numberStudentResult){
    int i = 0;
    for (i = 0; i < numberStudent; i++){
        if (l[i].Get_fname().find(key) != std::string::npos){
            result[numberStudentResult] = l[i];
            numberStudentResult++;
        }
    }
}

void Get_students_by_year(int key, Student l[], int numberStudent, Student result[], int &numberStudentResult){
    int i = 0;
    for (i = 0; i < numberStudent; i++){
        if (l[i].Get_year() == key){
            result[numberStudentResult] = l[i];
            numberStudentResult++;
        }
    }
}

void Get_students_by_program(string key, Student l[], int numberStudent, Student result[], int &numberStudentResult){
    int i = 0;
    for (i = 0; i < numberStudent; i++){
        if (l[i].Get_program() == key){
            result[numberStudentResult] = l[i];
            numberStudentResult++;
        }
    }
}

void Get_students_by_classname(string key, Student l[], int numberStudent, Student result[], int &numberStudentResult){
    int i = 0;
    for (i = 0; i < numberStudent; i++){
        if (l[i].Get_classname() == key){
            result[numberStudentResult] = l[i];
            numberStudentResult++;
        }
    }
}

#endif // STUDENT_LIB_H_
